﻿package Modules.MouseCursorModule
{
	import Modules.MainTimeLine;
	import Modules.Utilities;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.Point;
	
	public class BuildTool extends Tool
	{
		public var tempFarmObject:Bitmap;
		public var offsetX:Number;
		public var offsetY:Number;
		public var intercept:Boolean;
		
		public function BuildTool()
		{
			super();
			
			tempFarmObject = MainTimeLine.biggrid;
			MainTimeLine.biggrid.visible = true;
			tempFarmObject.alpha = 0.3;
			
			tempFarmObject.bitmapData.applyFilter(tempFarmObject.bitmapData, tempFarmObject.bitmapData.rect, new Point(), new ColorMatrixFilter(
				[0, 0.75, 0.75,0, //red
					0,0.75,0.75,0.75, //green
					0,0, 0.75,0.75,   //blue
					0.75, 0.75, 0.75,0.75,
					0,0,1, 0]));
			
			offsetX = tempFarmObject.width/2;
			offsetY = tempFarmObject.height/2;
		}
		
		override public function toolUp(event:MouseEvent):void{
			
			super.toolUp(event);
			
			if(click){
				trace("intercept is "+intercept);
				if(!intercept){
					//Offload this part to avatar
					var newObject:MovieClip = new BuildingDisplayObject();
					MainTimeLine.farmcontainer.addChild(newObject);
					newObject.x = tempFarmObject.x;
					newObject.y = tempFarmObject.y;
					MainTimeLine.farmcontainer.getFarmObjects();
				}
			}else{
				trace("exceedeed click time limit");
			}
		}
		
		override public function toolOver(event:MouseEvent):void{
			tempFarmObject.visible = true;
		}
		
		override public function toolMove(event:MouseEvent):void{
			
			tempFarmObject.x = event.localX-offsetX;
			tempFarmObject.y = event.localY-offsetY;
			
			if(!(event.target is Farm)){
				tempFarmObject.x += event.target.x;
				tempFarmObject.y += event.target.y;
			}
			
			tempFarmObject.x = Utilities.getNearestGridX(tempFarmObject.x);
			tempFarmObject.y = Utilities.getNearestGridY(tempFarmObject.y);
			
			intercept = !MainTimeLine.activeFarm.checkGridBounds(tempFarmObject.x, tempFarmObject.y);
			
			var i:int = 0;
			var currentObject:MovieClip;
			
			if(!intercept){
				for(i=0; i< MainTimeLine.numFarmObjects; i++){
					
					currentObject = MainTimeLine.farmObjects[i];
					if(currentObject.grid != null){
						
						if(currentObject.grid.hitTest(new Point(currentObject.x, currentObject.y),255,tempFarmObject,new Point(tempFarmObject.x, tempFarmObject.y),	255)){
							intercept = true;
							break;
						}else{
							intercept = false;
						}
					}else{
						intercept = false;
					}
				}
			}
			
			if(intercept){
				tempFarmObject.bitmapData.applyFilter(tempFarmObject.bitmapData, tempFarmObject.bitmapData.rect, new Point(), new ColorMatrixFilter([1, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]));
			}else{
				tempFarmObject.bitmapData.applyFilter(tempFarmObject.bitmapData, tempFarmObject.bitmapData.rect, new Point(), new ColorMatrixFilter([0.75, 0.75, 0.75, 0, 0,0.75, 0.75, 0.75, 0, 0, 0.75, 0.75, 0.75, 0, 0, 0, 0, 0, 1, 0]));
			}
		}
	}
	
	
}